package net.dreamlu.qa.controller;

import java.util.Date;

import net.dreamlu.qa.interceptor.UserInterceptor;
import net.dreamlu.qa.model.MailVerify;
import net.dreamlu.qa.model.User;
import net.dreamlu.qa.validator.ForgotValidator;
import net.dreamlu.qa.validator.LoginValidator;
import net.dreamlu.qa.validator.SettingValidator;
import net.dreamlu.qa.validator.UserValidator;
import net.dreamlu.util.MD5Util;
import net.dreamlu.util.MailUtil;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StringKit;
import com.jfinal.upload.UploadFile;

public class UserController extends Controller {
	
	// 首页
	public void index() {
		redirect("../index", true);
	}
	// 注册
	public void register() {
		render("/account/register.jsp");
	}
	
	// 注册提交-->发送mail
	@Before(UserValidator.class)
	public void regSub() {
		User user = getModel(User.class);
		String basePath = getPara("basePath");
		String email = user.getStr("email");
		// 保存用户信息
		user.addUser(user.set("password", MD5Util.getMD5Str(user.getStr("password"))));
		String code = MD5Util.getMD5Str((new Date()) + "");
		// 添加邮件认证信息
		new MailVerify().set("user_id", user.get("id")).set("verify_code", code).save();
		// 发送邮件
		MailUtil.emailSend(basePath, user.getStr("login_name"), email, basePath + "user/finish?code=" + code);
		setAttr("user", user);
		setAttr("mailUrl", MailUtil.mailAdd(email));
		setAttr("operate", "注册");
		setAttr("action", "激活");
		render("/account/mail_verify.jsp");
	}
	
	// 邮件校验通过
	public void finish(){
		MailVerify mailVerify = MailVerify.dao.findFirst("select * from mail_verify where verify_code = ?", getPara("code"));
		if(mailVerify != null){
			User user = User.dao.findById(mailVerify.get("user_id"));
			user.set("email_verify", 1).set("update_time", new Date()).update();
			setSessionAttr("user", user);
			index();
		}else {
			render("/error/error.jsp");
		}
	}
	
	// 登录
	public void login() {
		render("/account/login.jsp");
	}
	
	// 登录sub
	@Before(LoginValidator.class)
	public void session() {
		User user = getModel(User.class);
		User u = User.dao.findFirst("select * from user_info where email = ? and password = ? and email_verify=1", user.get("email"), MD5Util.getMD5Str(user.getStr("password")));
		u.updateUser(u.set("status", 1).set("recent_login_time", new Date()));
		setSessionAttr("user", u);
		index();
	}
	
	//登出
	public void logout() {
		User user = getSessionAttr("user");
		user.updateUser(user.set("status", 0));
		removeSessionAttr("user");
		index();
	}
	
	// 完善个人资料
	@Before(UserInterceptor.class)
	public void settings() {
		String tab = getPara("tab");
		User user = getModel(User.class);
		if(user != null && tab != null){
			User u = getSessionAttr("user");
			u.updateUser(u.setAttrs(user));
			setAttr("user", user);
			setAttr("setting_msg", "1");
		}
		if(StringKit.notBlank(tab)){
			setAttr("tab", tab);
		}
		render("/account/settings.jsp");
	}
	
	// 更改密码
	@Before({UserInterceptor.class, SettingValidator.class})
	public void settingPwd() {
		User user = getSessionAttr("user");
		user.updateUser(user.set("password", MD5Util.getMD5Str(getPara("password"))));
		removeSessionAttr("user");
		index();
	}
	
	// 头像上传
	@Before(UserInterceptor.class)
	public void upload() {
		try {
			UploadFile file = getFile("uphoto", PathKit.getWebRootPath() + "/photos" , 1024 * 1024);
			User user = getSessionAttr("user");
			user.updateUser(user.set("head_photo", "photos/" + file.getFileName()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		render("/account/settings.jsp");
	}
	
	// 找回密码
	public void forgot(){
		if(getSessionAttr("user") == null){
			render("/account/forgot.jsp");
		}else{
			index();
		}
	}
	
	// 找回密码提交
	@Before(ForgotValidator.class)
	public void forgotSub() {
		String email = getPara("mail");
		System.out.println("mail:" + email);
		String basePath = getPara("basePath");
		User user = User.dao.findFirst("select * from user_info where email = ?", email);
		if(user == null){
			setAttr("errorMail", "1");
			render("/account/forgot.jsp");
		}else {
			String code = MD5Util.getMD5Str((new Date()) + "");
			// 添加邮件认证信息
			new MailVerify().set("user_id", user.get("id")).set("verify_code", code).save();
			// 发送邮件
			MailUtil.emailSend(basePath, user.getStr("login_name"), email, basePath + "user/finish?code=" + code);
			setAttr("user", user);
			setAttr("mailUrl", MailUtil.mailAdd(email));
			setAttr("operate", "重置");
			setAttr("action", "重置");
			render("/account/mail_verify.jsp");
		}
	}
}	